package org.yn.mkt.service.impl;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.yn.emp.domain.Employee;
import org.yn.mkt.domain.*;
import org.yn.mkt.dto.ClueBatchDTO;
import org.yn.mkt.dto.TransferBusinessDTO;
import org.yn.mkt.mapper.*;
import org.yn.mkt.query.ClueQuery;
import org.yn.mkt.service.IClueService;
import org.yn.base.service.impl.BaseServiceImpl;
import org.springframework.stereotype.Service;
import org.yn.mkt.vo.ClueResourceVO;
import org.yn.mkt.vo.ClueVO;

import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yn
 * @since 2023-07-31
 */
@Service
public class ClueServiceImpl extends BaseServiceImpl<Clue> implements IClueService {
    @Autowired
    private ClueMapper clueMapper;

    @Autowired
    private ClueRemarkMapper clueRemarkMapper;


    @Autowired
    private BusinessMapper businessMapper;
    @Autowired
    private BusinessRemarkMapper businessRemarkMapper;
    @Autowired
    private BusinessProductMapper businessProductMapper;
    @Override
    public void handleAssign(Clue clue, Employee loginUser) {
        //1. 改线索状态  分配给某个人之后 需要改状态
        clue.setState(1);
        // 2.更新线索
        clueMapper.update(clue);
        // 3.添加备注
        ClueRemark clueRemark = new ClueRemark(); //Nwe一个新的备注对象
        clueRemark.setClueId(clue.getId()); //线索备注绑定线索id

        clueRemark.setCreateBy(loginUser);//获取当前用户的对象
        String content = "%s分配%s给%s";  // 用户xx 分配 客户名  给 用户yy
        clueRemark.setNoteContent(String.format(content,loginUser.getUsername(),clue.getFullName(),clue.getOwner().getUsername()));
        clueRemarkMapper.insert(clueRemark);

    }

    @Override
    public void batchAssign(ClueBatchDTO clue, Employee loginUser) {
        // 1.批量分配信息  状态肯定会变， 在sql中写更方便
        clueMapper.batchAssign(clue);



        // 备注记录
        for (Long id:clue.getCluesId()) {
            ClueRemark clueRemark = new ClueRemark();
            clueRemark.setClueId(id);
            clueRemark.setCreateBy(loginUser);
            String content = "%s分配%s给%s";
            clueRemark.setNoteContent(String.format(content,loginUser.getUsername(),id,clue.getOwner().getUsername()));
            clueRemarkMapper.insert(clueRemark);
        }
    }

    @Override
    public void transferBusiness(TransferBusinessDTO transferBusinessDTO, Employee loginUser) {
    // 修改状态
        //获取这个transferBusinessDTO的 business的id 再获取线索clue对象
        //修改clue的对象
        Clue clue = clueMapper.selectById(transferBusinessDTO.getId());
        clue.setState(2);

    //线索备注
        ClueRemark clueRemark = new ClueRemark();
        clueRemark.setClueId(clue.getId());
        clueRemark.setCreateBy(loginUser);
        String content = "%s转化为商机,操作人:%s";
        clueRemark.setNoteContent(String.format(content,clue.getFullName(),loginUser.getUsername()));

        //线索时间
        clueRemark.setCreateTime(new Date());

        // 添加修改人
        clueRemark.setCreateBy(loginUser);

        clueRemarkMapper.insert(clueRemark);

        // 商机添加
        Business business = new Business();

        String str = "%s对%s感兴趣";
        business.setName(String.format(str,clue.getFullName(),transferBusinessDTO.getProduct().getName()));
        business.setClueId(clue.getId());
        business.setProductId(transferBusinessDTO.getProduct().getId());
        business.setProductPrice(transferBusinessDTO.getProduct().getPrice());
        business.setProductName(transferBusinessDTO.getProduct().getName());
        business.setState(0);
        businessMapper.insert(business);
        // 商机备注
        BusinessRemark businessRemark = new BusinessRemark();

        String str1 = "%s添加了商机:%s";
        businessRemark.setNoteContent(String.format(str1,loginUser.getUsername(),business.getName()));
        businessRemark.setEmployee(loginUser);
//        businessRemark.setCreateTime(new Date());
        businessRemark.setBisinessId(business.getId());
        businessRemarkMapper.insert(businessRemark);
        // 商机产品
        BusinessProduct businessProduct = new BusinessProduct();
        BeanUtils.copyProperties(transferBusinessDTO.getProduct(),businessProduct);
        businessProduct.setId(null);
        businessProduct.setBusinessId(business.getId());
        businessProductMapper.insert(businessProduct);


    }

    @Override
    public void batchInsert(List<ClueVO> clueList) {
        clueMapper.batchInsert(clueList);
    }

    @Override
    public List<ClueResourceVO> getClueResource() {
        return clueMapper. getClueResource();
    }

    @Override
    public List<Clue> queyDowmloadExcel(ClueQuery query) {
        return clueMapper.queyDowmloadExcel(query);
    }

}
